﻿using System;
using System.Data;
using System.EnterpriseServices;
using System.Linq;
using System.Web.UI.WebControls;
using NetDon.OA.Portal;


namespace NetDon.OA.Portal.Controls
{
    public partial class AddressEditor : UserControlBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack&&Visible)
            {
                CountryId = ddlCountry.SelectedValue;
                StateId = ddlState.SelectedValue;
                markAddressLine1.Visible = !Optional;
                markPostCode.Visible = !Optional;
                markCountry.Visible = !Optional;
                markState.Visible = !Optional;
            }
        }

        public Guid? AddressId
        {
            get
            {
                object o = ViewState["AddressId"];
                return o == null ? null : (Guid?)o;
            }
            set
            {
                if (value != AddressId)
                {
                    ViewState["AddressId"] = value;
                    LoadAddress();
                }
            }
        }

        public string Caption
        {
            get
            {
                return (string)ViewState["Caption"] ?? string.Empty;
            }
            set
            {
                ViewState["Caption"] = value;
            }
        }

        public bool Optional
        {
            get
            {
                object o = ViewState["Optional"];
                return o == null || (bool) o;
            }
            set
            {
                ViewState["Optional"] = value;
            }
        }

        private string CountryId
        {
            get
            {
                return (string) ViewState["CountryId"];
            }
            set
            {
                ViewState["CountryId"] = value;
            }
        }

        private string StateId
        {
            get
            {
                return (string)ViewState["StateId"];
            }
            set
            {
                ViewState["StateId"] = value;
            }
        }

        private void LoadAddress()
        {
           
        }

        public bool Save()
        {
            return true;
        }

        private bool Validate()
        {

            return true;

        }

        private bool HasData()
        {
            if (!string.IsNullOrEmpty(txtAddressLine1.Text.Trim()))
            {
                return true;
            }
            if (!string.IsNullOrEmpty(txtAddressLine2.Text.Trim()))
            {
                return true;
            }
            if (!string.IsNullOrEmpty(ddlCountry.SelectedValue))
            {
                return true;
            }
            if (!string.IsNullOrEmpty(ddlState.SelectedValue))
            {
                return true;
            }
            if (!string.IsNullOrEmpty(txtCity.Text.Trim()))
            {
                return true;
            }
            if (!string.IsNullOrEmpty(txtPostCode.Text.Trim()))
            {
                return true;
            }
            return false;
        }

        public bool Save(ITransaction transaction)
        {
            return true;
        }

        protected override void OnPreRender(EventArgs e)
        {
            ddlCountry_CascadingDropDown.SelectedValue = CountryId;
            ddlState_CascadingDropDown.SelectedValue = StateId;
            ScriptStartup(string.Format("Sys.Application.add_load(function(){{$find('{0}').add_populated({1});}});", ddlState_CascadingDropDown.BehaviorID,
                                        ClientID + "_StatesPopulated"));
            base.OnPreRender(e);
        }
    }
}